﻿using System;
using System.Windows;
using System.Deployment.Application;
using CCF20Model;


namespace CCF20
{
    /// <summary>
    /// Interaction logic for Configuration.xaml
    /// </summary>
    public partial class Configuration : Window
    {
        AppConfig myConfig;
        string dbname,username,password,logdir,catalog,zicurl;
        bool isjava = false;
        public Configuration()
        {
            InitializeComponent();
            string myConfigPath;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                myConfigPath = ApplicationDeployment.CurrentDeployment.DataDirectory;
                
            }
            else
            {
                myConfigPath = ".";
            }

            myConfigPath += "\\CCFConfiguration.xml";
            LoggerHelper.LogCCF20(string.Format("Configuration Update {0}",myConfig));
            myConfig = new AppConfig(myConfigPath);
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                dbname = myConfig.GetValue("database", "appSettings", "value");
                username = myConfig.GetValue("user", "appSettings", "value");
                password = myConfig.GetValue("password", "appSettings", "value");
                logdir = myConfig.GetValue("logdir", "appSettings", "value");
                catalog = myConfig.GetValue("catalog", "appSettings", "value");
                zicurl = myConfig.GetValue("zicurl", "appSettings", "value");
                if (!bool.TryParse(myConfig.GetValue("isjava", "appSettings", "value"), out isjava))
                {
                    MessageBox.Show("一汽大众设置出错");
                }
                
            }));
            
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            if ((this.DBName.Text.Trim() != dbname) ||
                (this.UserName.Text.Trim() != username) ||
                (this.UserPwd.Text.Trim() != password) ||
                (this.CatalogName.Text.Trim() != catalog) ||
                (this.ZICUrl.Text.Trim() != zicurl) ||
                (this.IsJava.IsChecked != !isjava) ||
                (this.LogDir.Text.Trim() != logdir))
            {
                myConfig.SetValue("database", "appSettings", "value", this.DBName.Text.Trim());
                myConfig.SetValue("user", "appSettings", "value", this.UserName.Text.Trim());
                myConfig.SetValue("password", "appSettings", "value", this.UserPwd.Text.Trim());
                myConfig.SetValue("logdir", "appSettings", "value", this.LogDir.Text.Trim());
                myConfig.SetValue("catalog", "appSettings", "value", this.CatalogName.Text.Trim());
                myConfig.SetValue("zicurl", "appSettings", "value", this.ZICUrl.Text.Trim());
                myConfig.SetValue("isjava", "appSettings", "value", (!this.IsJava.IsChecked).ToString());
                myConfig.SaveConfigDoc();
                App.CheckConfiguration();
                App.Current.Shutdown();

            }
            else
            {
                this.Close();
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.DBName.Text = dbname;
                this.UserName.Text = username;
                this.UserPwd.Text = password;
                this.LogDir.Text = logdir;
                this.CatalogName.Text = catalog;
                this.ZICUrl.Text = zicurl;
                this.IsJava.IsChecked = !isjava;
            }));
            
        }
    }
}
